﻿using System.Collections.Generic;
using System.Threading;
using System.Windows;
using KV_WSN;
using KV_WSN.Sensor;

namespace SmartShopping
{
    public class Global
    {
        public static Coordinator coordinator = new Coordinator();
        public static bool isOpen=false;
        /// <summary>
        /// 基类对象
        /// </summary>
        public static SensorBase smoke = new SensorBase();

        /// <summary>
        /// 红外人体测温传感器
        /// </summary>
        public static SensorBase Infrared = new SensorBase();

        /// <summary>
        /// 设备列表
        /// </summary>
        public static List<SensorBase> coorSensors = new List<SensorBase>();

        /// <summary>
        /// 获取设备
        /// </summary>
        /// <param name="sensorList"></param>
        public static void Coordinator_ReceiveSensorEvent(List<SensorBase> sensorList)
        {
            coorSensors = sensorList;
            SetType();
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static bool OpenPortZigbee(string port)
        {
            if (!coordinator.OpenSerialPort(port))
            {
                return false;
            }

            coordinator.ReceiveSensorEvent +=
                new ParseData.DelegateReceiveSensor(Coordinator_ReceiveSensorEvent);
            isOpen = true;
            return true;
        }

        /// <summary>
        /// 设置每个设备类型
        /// </summary>
        private static void SetType()
        {
            foreach (SensorBase sensor in coorSensors)
            {
                //0x40烟雾传感器
                if (sensor.Type == 0x40)
                {
                    smoke = sensor;
                }
                //0x50红外人体测温
                else if (sensor.Type == 0x50)
                {
                    Infrared = sensor;
                }
            }
        }
    }
}